package com.zf.mall.user.service.ser.impl;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zf.mall.common.response.ErrorEnum;
import com.zf.mall.common.response.ResponseResultModel;
import com.zf.mall.common.util.JwtTokenUtil;
import com.zf.mall.common.util.MD5Utils;
import com.zf.mall.user.ds.custom.mapper.CustomUserMapper;
import com.zf.mall.user.ds.entity.User;
import com.zf.mall.user.ds.mapper.UserMapper;
import com.zf.mall.user.service.ser.UserService;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class UserServiceImpl extends BaseServiceImpl<UserMapper, User,CustomUserMapper> implements UserService {

	

	@Transactional // 如果这个地方有其它库的操作，需要加入分布式事务控制GlobalTransactional，以及配置相关属性
	public ResponseEntity<?> register(User user) {
		if (user == null || StrUtil.isBlank(user.getPassword())) {
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C22001), HttpStatus.OK);
		}
		if (StrUtil.isBlank(user.getUserMobile())) {
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C22002), HttpStatus.OK);
		}
		//User _user = getOne(new QueryWrapper<>(user));
		LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();
		lambda.eq(User::getUserMobile, user.getUserMobile());
		List<User> list = list(lambda);
		
		if (CollectionUtil.isNotEmpty(list)) {
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C22000), HttpStatus.OK);
		}

		if (StrUtil.isNotBlank(user.getPassword())) {
			user.setPassword(MD5Utils.md5(user.getPassword(), user.getUserMobile()));
		}

		if (save(user)) {
			return new ResponseEntity<>(new ResponseResultModel<User>(ErrorEnum.C10000, user), HttpStatus.OK);
		} else {
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	public ResponseEntity<?> login(User user) {

		if(user == null || StrUtil.isBlank(user.getUserMobile()) || StrUtil.isBlank(user.getPassword())) {
			//log.info("SERVER: 请求参数 user : {}" , user);
			//log.info("SERVER: 请求参数 userMobile : {}" , user.getUserMobile());
			//log.info("SERVER: 请求参数 password : {}" , user.getPassword());
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C22001), HttpStatus.OK);
		}
		// 这个地方最好用REDIS缓存用户信息，从缓存获取用户信息进行比较，目前没有REDIS服务，暂时从MYSQL库里面进行查询比对

		LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();
		lambda.eq(User::getUserMobile, user.getUserMobile());
		List<User> list = list(lambda);
		if(CollectionUtil.isEmpty(list)) {
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C22003), HttpStatus.OK);
		}
		if(list.size() > 1) {
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C22004), HttpStatus.OK);
		}
		User _user = list.get(0);
		if(StrUtil.isBlank(_user.getPassword())) {
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C22005), HttpStatus.OK);
		}
		
		if(!StrUtil.equals(MD5Utils.md5(user.getPassword(), user.getUserMobile()) , _user.getPassword())) {
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C22006), HttpStatus.OK);
		}
		
		Long userId = _user.getId();
		String userToken ;
		try {
			userToken = JwtTokenUtil.createToken(userId);
			return new ResponseEntity<>(new ResponseResultModel<String>(ErrorEnum.C10000 , userToken), HttpStatus.OK);
		}catch (Exception e) {
			log.error("用户登录时,生成用户TOKEN错误,详情: {} " , e);
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	public ResponseEntity<?> wechatLogin(User user) {
		
		//演示自定义是查询的使用方法
		List<User> list = getCustomMapper().selectUserByMobile(user);
		return new ResponseEntity<>(new ResponseResultModel<List<User>>(ErrorEnum.C10000,list),HttpStatus.OK);
	}

}
